package com.yjha.lzmarathonsys.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yjha.lzmarathonsys.bean.*;
import com.yjha.lzmarathonsys.dto.Param;
import com.yjha.lzmarathonsys.mapper.TbSpecialClockinMapper;
import com.yjha.lzmarathonsys.service.*;
import com.yjha.lzmarathonsys.utils.*;
import com.yjha.lzmarathonsys.vo.TbSpecialClockinVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;


@Service("tbSpecialClockinService")
public class TbSpecialClockinServiceImpl extends ServiceImpl<TbSpecialClockinMapper, TbSpecialClockin> implements TbSpecialClockinService {

	@Value("${lzmarathonsys.specialclockin.codePrefix}")
	private String codePrefix;
	@Value("${lzmarathonsys.specialclockin.getmyZtClockin}")
	private String getmyZtClockin;
	@Value("${lzmarathonsys.specialclockin.getZtAll}")
	private String getZtAll;
	@Value("${lzmarathonsys.specialclockin.getZtList}")
	private String getZtList;
	@Autowired
	private TbCommentService tbCommentService;
	@Autowired
	private TbLikeService tbLikeService;
	@Autowired
	private TbImageslService tbImageslService;
	@Autowired
	private TbUserInfoService tbUserInfoService;
	@Autowired
	private FileUploadUtil fileUploadUtil;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private TbSpecialClockinMapper tbSpecialClockinMapper;
	@Autowired
	private RedisUtils redisUtils;
	@Autowired
	private TbSpecialService tbSpecialService;
	@Autowired
	private TbSpecialClockinMapper tbspecialclockinMapper;

	@Override
	public R queryPage(Param params) {
		// PageUtils pageUtils = null;
		// boolean hasKey = redisUtils.hasKey(getZtList);
		// if (!hasKey) {
		Map<String, Object> param = new HashMap<>();
		String page1 = params.getPage();
		String limit1 = params.getLimit();
		param.put("page", page1);
		param.put("limit", limit1);
		String tbUserId = params.getKey();
		MPJLambdaWrapper<TbSpecialClockin> tbSpecialClockinVoMPJLambdaWrapper = new MPJLambdaWrapper<>();
		tbSpecialClockinVoMPJLambdaWrapper.select(TbUserInfo::getTbUsername, TbUserInfo::getTbImage).select(TbSpecialClockin::getTbId, TbSpecialClockin::getTbUuid, TbSpecialClockin::getTbLocation, TbSpecialClockin::getTbCreatetime, TbSpecialClockin::getTbLongitudeLatitude, TbSpecialClockin::getTbStatus, TbSpecialClockin::getTbTestimonials, TbSpecialClockin::getTbSpecialid).select(TbSpecial::getTbSpecialname, TbSpecial::getTbId).leftJoin(TbUserInfo.class, TbUserInfo::getTbWxuuid, TbSpecialClockin::getTbUserid).leftJoin(TbSpecial.class, TbSpecial::getTbId, TbSpecialClockin::getTbSpecialid);
		if (params.getTbCreatetime() != null && !"".equals(params.getTbCreatetime())) {
			tbSpecialClockinVoMPJLambdaWrapper.eq("t.tb_createtime", params.getTbCreatetime());
		}
		if (params.getTbStatus() != null && params.getTbStatus() != "") {
			tbSpecialClockinVoMPJLambdaWrapper.eq("t.tb_status", params.getTbStatus());
		}
		IPage<TbSpecialClockinVo> page = tbSpecialClockinMapper.selectJoinPage(new Page<>(Long.parseLong((String) param.get("page")), Long.parseLong((String) param.get("limit"))), TbSpecialClockinVo.class, tbSpecialClockinVoMPJLambdaWrapper);
		PageUtils pageUtils = new PageUtils(page);
		List<TbSpecialClockinVo> collect = page.getRecords().stream().map(tbSpecialClockinVo -> {
			QueryWrapper<TbUserInfo> tbUserInfoQueryWrapper = new QueryWrapper<>();
			tbUserInfoQueryWrapper.eq("tb_wxuuid", tbSpecialClockinVo.getTbUserid());
			TbUserInfo tbUserInfo = tbUserInfoService.getOne(tbUserInfoQueryWrapper);
			QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<TbImagesl>().eq("tb_special_uuid", tbSpecialClockinVo.getTbUuid());
			List<TbImagesl> tbImageslList = tbImageslService.list(tbImageslQueryWrapper);
			QueryWrapper<TbLike> likeQueryWrapper = new QueryWrapper<TbLike>().eq("target_id", tbSpecialClockinVo.getTbId());
			long count = tbLikeService.count(likeQueryWrapper);
			QueryWrapper<TbComment> tbCommentQueryWrapper = new QueryWrapper<TbComment>().eq("tb_target_id", tbSpecialClockinVo.getTbId());
			Long counted = tbCommentService.count(tbCommentQueryWrapper);
			tbSpecialClockinVo.setTbUsername(tbUserInfo.getTbUsername());
			tbSpecialClockinVo.setTbImage(tbUserInfo.getTbImage());
			tbSpecialClockinVo.setLikeNum(count);
			tbSpecialClockinVo.setCommentNum(counted);
			tbSpecialClockinVo.setTbImageslList(tbImageslList);
			return tbSpecialClockinVo;
		}).collect(Collectors.toList());
		pageUtils.setList(collect);
		//     try {
		// 	    redisUtils.set(getZtList, pageUtils,30);
		//     } catch (Exception e) {
		// 	    throw new RuntimeException(e);
		//     }
		// }else{
		//     pageUtils = (PageUtils) redisUtils.get(getZtList);
		// }
		return R.ok().put("page", pageUtils);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public R saveTbSpecialClockin(TbSpecialClockin tbSpecialClockin, MultipartFile[] files) throws Exception {
		String tokenFromRequest = JwtUtils.getTokenFromRequest(request);
		String openId = JwtUtils.getOpenId(tokenFromRequest);
		String uuid = UUID.randomUUID().toString().replace("-", "");
		tbSpecialClockin.setTbUuid(uuid);
		tbSpecialClockin.setTbUserid(openId);
		this.save(tbSpecialClockin);
		List<String> uploadedUrls = new ArrayList<>();

		for (MultipartFile file : files) {
			// 类型校验
			String s;
			if ("docx".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename()))) {
				s = fileUploadUtil.validateFile(file, "WD");
			} else {
				s = fileUploadUtil.validateFile(file, "TP");
			}

			if (s != null) {
				return R.error("文件校验失败：" + s);
			}

			// 构建路径
			String suffix = FileUtil.getSuffix(file.getOriginalFilename());
			String fileName = StrUtil.uuid().replaceAll("-", "") + "." + suffix;
			String uploadDir = fileUploadUtil.getWinOrLinuxUploadFilePath("tp");
			String fullPath = uploadDir + fileName;

			// 创建目录
			fileUploadUtil.createDirectory(uploadDir);

			// 保存文件
			file.transferTo(FileUtil.file(fullPath));

			// 构建 URL 返回
			String fileUrl = "https://jtfx.lzu.edu.cn/api/upload/tp/" + fileName;
			uploadedUrls.add(fileUrl);
		}
		for (int i = 0; i < uploadedUrls.size(); i++) {
			TbImagesl tbImagesl = new TbImagesl();
			tbImagesl.setTbImage(uploadedUrls.get(i));
			tbImagesl.setTbSpecialUuid(uuid);
			tbImageslService.save(tbImagesl);
		}
		return R.ok();
	}

	@Override
	public R getmyZtClockinById(Param params) {
		// PageUtils pageUtils = null;
		// boolean hasKey = redisUtils.hasKey(getmyZtClockin);
		// if (!hasKey) {
		String tokenFromRequest = JwtUtils.getTokenFromRequest(request);
		String temp = JwtUtils.getOpenId(tokenFromRequest);
		Map<String, Object> param = new HashMap<>();
		String page1 = params.getPage();
		String limit1 = params.getLimit();
		param.put("page", page1);
		param.put("limit", limit1);
		String tbUserId = params.getKey();
		MPJLambdaWrapper<TbSpecialClockin> tbSpecialClockinVoMPJLambdaWrapper = new MPJLambdaWrapper<>();
		tbSpecialClockinVoMPJLambdaWrapper.select(TbUserInfo::getTbUsername, TbUserInfo::getTbImage).select(TbSpecialClockin::getTbId, TbSpecialClockin::getTbUuid, TbSpecialClockin::getTbLocation, TbSpecialClockin::getTbCreatetime, TbSpecialClockin::getTbLongitudeLatitude, TbSpecialClockin::getTbStatus, TbSpecialClockin::getTbTestimonials, TbSpecialClockin::getTbSpecialid).select(TbSpecial::getTbSpecialname, TbSpecial::getTbId).leftJoin(TbUserInfo.class, TbUserInfo::getTbWxuuid, TbSpecialClockin::getTbUserid).leftJoin(TbSpecial.class, TbSpecial::getTbId, TbSpecialClockin::getTbSpecialid).eq(TbSpecialClockin::getTbUserid, temp).and(tw -> tw.eq(TbSpecialClockin::getTbSpecialid, params.getSpecialid()));
		IPage<TbSpecialClockinVo> page = tbSpecialClockinMapper.selectJoinPage(new Page<>(Long.parseLong((String) param.get("page")), Long.parseLong((String) param.get("limit"))), TbSpecialClockinVo.class, tbSpecialClockinVoMPJLambdaWrapper);
		PageUtils pageUtils = new PageUtils(page);
		List<TbSpecialClockinVo> collect = page.getRecords().stream().map(tbSpecialClockinVo -> {
			QueryWrapper<TbLike> tbLikeQueryWrapper = new QueryWrapper<>();
			QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<TbImagesl>().eq("tb_special_uuid", tbSpecialClockinVo.getTbUuid());
			List<TbImagesl> tbImageslList = tbImageslService.list(tbImageslQueryWrapper);
			QueryWrapper<TbLike> likeQueryWrapper = new QueryWrapper<TbLike>().eq("target_id", tbSpecialClockinVo.getTbUuid());
			long count = tbLikeService.count(likeQueryWrapper);
			QueryWrapper<TbComment> tbCommentQueryWrapper = new QueryWrapper<TbComment>().eq("tb_target_id", tbSpecialClockinVo.getTbId()).and(tw -> {
				tw.eq("tb_type", 1);
			}).and(tw -> tw.eq("tb_target_status", 1));
			Long counted = tbCommentService.count(tbCommentQueryWrapper);
			QueryWrapper<TbLike> and = tbLikeQueryWrapper.eq("target_id", tbSpecialClockinVo.getTbUuid()).and(tbLikeQueryWrapper1 -> {
				tbLikeQueryWrapper1.eq("user_id", temp);
			});
			TbLike tbLike = tbLikeService.getOne(and);
			if (tbLike != null) {
				tbSpecialClockinVo.setClockStatus(true);
			} else {
				tbSpecialClockinVo.setClockStatus(false);
			}
			tbSpecialClockinVo.setLikeNum(count);
			tbSpecialClockinVo.setCommentNum(counted);
			tbSpecialClockinVo.setTbImageslList(tbImageslList);
			return tbSpecialClockinVo;
		}).sorted((o1, o2) -> o2.getTbCreatetime().compareTo(o1.getTbCreatetime())).collect(Collectors.toList());
		pageUtils.setList(collect);
		// 	try {
		// 		redisUtils.set(getmyZtClockin, pageUtils,30);
		// 	} catch (Exception e) {
		// 		throw new RuntimeException(e);
		// 	}
		// }else{
		// 	pageUtils = (PageUtils) redisUtils.get(getmyZtClockin);
		// }
		return R.ok().put("page", pageUtils);
	}

	@Override
	public R getAll(Param params) {
		// PageUtils pageUtils = null;
		// IPage<TbSpecialClockinVo> page = null;
		// boolean hasKey = redisUtils.hasKey(getZtAll);
		// if (!hasKey) {
		// String token = (String) request.getSession().getAttribute("token");
		String tokenFromRequest = JwtUtils.getTokenFromRequest(request);
		String temp = JwtUtils.getOpenId(tokenFromRequest);
		Map<String, Object> param = new HashMap<>();
		String page1 = params.getPage();
		String limit1 = params.getLimit();
		List<String> longitudeLatitudeList = params.getLongitudeLatitudeList();
		// longitudeLatitudeList.add(params.getLongitudeLatitude());
		param.put("page", page1);
		param.put("limit", limit1);
		MPJLambdaWrapper<TbSpecialClockin> tbSpecialClockinVoMPJLambdaWrapper = new MPJLambdaWrapper<>();
		tbSpecialClockinVoMPJLambdaWrapper.select(TbUserInfo::getTbUsername, TbUserInfo::getTbImage).select(TbSpecialClockin::getTbId, TbSpecialClockin::getTbUuid, TbSpecialClockin::getTbLocation, TbSpecialClockin::getTbCreatetime, TbSpecialClockin::getTbLongitudeLatitude, TbSpecialClockin::getTbStatus, TbSpecialClockin::getTbTestimonials, TbSpecialClockin::getTbSpecialid).select(TbSpecial::getTbSpecialname, TbSpecial::getTbId).leftJoin(TbUserInfo.class, TbUserInfo::getTbWxuuid, TbSpecialClockin::getTbUserid).leftJoin(TbSpecial.class, TbSpecial::getTbId, TbSpecialClockin::getTbSpecialid).eq(TbSpecialClockin::getTbStatus, 0).and(tw -> tw.in(TbSpecialClockin::getTbLongitudeLatitude, longitudeLatitudeList)).and(tw -> tw.eq(TbSpecialClockin::getTbSpecialid, params.getSpecialid()));
		Page<TbSpecialClockinVo> page = tbSpecialClockinMapper.selectJoinPage(new Page<>(Long.parseLong((String) param.get("page")), Long.parseLong((String) param.get("limit"))), TbSpecialClockinVo.class, tbSpecialClockinVoMPJLambdaWrapper);
		PageUtils pageUtils = new PageUtils(page);
		List<TbSpecialClockinVo> collect = page.getRecords().stream().map(tbSpecialClockinVo -> {
			QueryWrapper<TbLike> tbLikeQueryWrapper = new QueryWrapper<>();
			QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<TbImagesl>().eq("tb_special_uuid", tbSpecialClockinVo.getTbUuid());
			List<TbImagesl> tbImageslList = tbImageslService.list(tbImageslQueryWrapper);
			QueryWrapper<TbLike> likeQueryWrapper = new QueryWrapper<TbLike>().eq("target_id", tbSpecialClockinVo.getTbUuid());
			long count = tbLikeService.count(likeQueryWrapper);
			QueryWrapper<TbComment> tbCommentQueryWrapper = new QueryWrapper<TbComment>().eq("tb_target_id", tbSpecialClockinVo.getTbId()).and(tw -> {
				tw.eq("tb_type", 1);
			}).and(tw -> tw.eq("tb_target_status", 1));
			Long counted = tbCommentService.count(tbCommentQueryWrapper);
			QueryWrapper<TbLike> and = tbLikeQueryWrapper.eq("target_id", tbSpecialClockinVo.getTbUuid()).and(tbLikeQueryWrapper1 -> {
				tbLikeQueryWrapper1.eq("user_id", temp);
			});
			TbLike tbLike = tbLikeService.getOne(and);
			if (tbLike != null) {
				tbSpecialClockinVo.setClockStatus(true);
			} else {
				tbSpecialClockinVo.setClockStatus(false);
			}
			tbSpecialClockinVo.setLikeNum(count);
			tbSpecialClockinVo.setCommentNum(counted);
			tbSpecialClockinVo.setTbImageslList(tbImageslList);
			return tbSpecialClockinVo;
		}).sorted((o1, o2) -> o2.getTbCreatetime().compareTo(o1.getTbCreatetime())).collect(Collectors.toList());
		pageUtils.setList(collect);
		// 	try {
		// 		redisUtils.set(getZtAll, pageUtils,30);
		// 	} catch (Exception e) {
		// 		throw new RuntimeException(e);
		// 	}
		// }else{
		// 	pageUtils = (PageUtils) redisUtils.get(getZtAll);
		// 	if(pageUtils == null) {
		// 		pageUtils = new PageUtils(page);
		// 	}
		// }
		return R.ok().put("page", pageUtils);
	}

	@Override
	public R getLongitude(Integer tbSpecialid) {// List<String> longitudeLatitude) {
	// public R getLongitude() {// List<String> longitudeLatitude) {
	// 	Map<String, Object> tbPersonClockinVoHashMap = new HashMap<>();
	// 	List list = new ArrayList();
	// 	boolean hasKey = redisUtils.hasKey(codePrefix);
	// 	if (!hasKey) {
			QueryWrapper<TbSpecialClockin> tbSpecialClockinQueryWrapper = new QueryWrapper<>();
			tbSpecialClockinQueryWrapper.select("count(*) as count", "tb_specialid", "tb_longitude_latitude", "tb_location", "tb_specialid").eq("tb_status", 0).and(tw-> tw.eq("tb_specialid",tbSpecialid))
			// tbSpecialClockinQueryWrapper.select("count(*) as count", "tb_specialid", "tb_longitude_latitude", "tb_location", "tb_specialid").eq("tb_status", 0)
					// .in("tb_longitude_latitude", longitudeLatitude)  // 注意字段名和集合
					.groupBy("tb_longitude_latitude", "tb_location", "tb_specialid");
			List<Map<String, Object>> list1 = this.listMaps(tbSpecialClockinQueryWrapper);
			List<TbSpecialClockinVo> groupVoList = list1.stream().map(row -> {
				QueryWrapper<TbSpecial> tbSpecialQueryWrapper = new QueryWrapper<>();
				tbSpecialQueryWrapper.select("tb_id", "tb_specialname").eq("tb_id", row.get("tb_specialid"));
				TbSpecial tbSpecial = tbSpecialService.getOne(tbSpecialQueryWrapper);
				TbSpecialClockinVo vo = new TbSpecialClockinVo();
				vo.setCount(Long.parseLong(row.get("count").toString()));
				vo.setTbLongitudeLatitude((String) row.get("tb_longitude_latitude"));
				vo.setTbLocation((String) row.get("tb_location"));
				vo.setTbSpecialid(tbSpecial.getTbId());
				vo.setTbSpecialname(tbSpecial.getTbSpecialname());
				return vo;
			}).collect(Collectors.toList());
		// 	list.addAll(groupVoList);
		// 	tbPersonClockinVoHashMap.put("data", list);
		// 	redisUtils.hmset(codePrefix, tbPersonClockinVoHashMap, 30);
		// } else {
		// 	Map<Object, Object> map = redisUtils.hmget(codePrefix);
		// 	return R.ok().put("data", map);
		// }

		return R.ok().put("data", groupVoList);
	}

	@Override
	public synchronized void audit(TbSpecialClockin tbSpecialClockin) {
		UpdateWrapper<TbSpecialClockin> tbSpecialClockinUpdateWrapper = new UpdateWrapper<>();
		tbSpecialClockinUpdateWrapper.set("tb_status", tbSpecialClockin.getTbStatus()).eq("tb_id", tbSpecialClockin.getTbId());
		tbSpecialClockinMapper.update(tbSpecialClockin, tbSpecialClockinUpdateWrapper);
	}

	@Override
	public PageUtils myAllList(Param params) {
		Map<String, Object> map = new HashMap<>();
		String specialid = params.getSpecialid();
		String tbCreatetime = params.getTbCreatetime();
		String tbStatus = params.getTbStatus();
		String pages = params.getPage();
		String limit = params.getLimit();
		map.put("page", pages);
		map.put("limit", limit);
		MPJLambdaWrapper<TbSpecialClockin> tbSpecialClockinVoMPJLambdaWrapper = new MPJLambdaWrapper<>();
		tbSpecialClockinVoMPJLambdaWrapper.select(TbUserInfo::getTbUsername, TbUserInfo::getTbImage).select(TbSpecialClockin::getTbId, TbSpecialClockin::getTbUserid, TbSpecialClockin::getTbUuid, TbSpecialClockin::getTbLocation, TbSpecialClockin::getTbCreatetime, TbSpecialClockin::getTbLongitudeLatitude, TbSpecialClockin::getTbStatus, TbSpecialClockin::getTbTestimonials, TbSpecialClockin::getTbSpecialid).select(TbSpecial::getTbSpecialname, TbSpecial::getTbId).leftJoin(TbUserInfo.class, TbUserInfo::getTbWxuuid, TbSpecialClockin::getTbUserid).leftJoin(TbSpecial.class, TbSpecial::getTbId, TbSpecialClockin::getTbSpecialid).eq("tb_specialid", specialid);
		if (tbCreatetime != null && !"".equals(tbCreatetime)) {
			tbSpecialClockinVoMPJLambdaWrapper.and(tw -> {
				tw.eq(TbSpecialClockin::getTbCreatetime, tbCreatetime);
			});
		}
		if (tbStatus != null && !"".equals(tbStatus)) {
			tbSpecialClockinVoMPJLambdaWrapper.and(tw -> {
				tw.eq(TbSpecialClockin::getTbStatus, tbStatus);
			});
		}
		IPage<TbSpecialClockinVo> page = tbSpecialClockinMapper.selectJoinPage(new Page<>(Long.parseLong((String) map.get("page")), Long.parseLong((String) map.get("limit"))), TbSpecialClockinVo.class, tbSpecialClockinVoMPJLambdaWrapper);
		PageUtils pageUtils = new PageUtils(page);
		List<TbSpecialClockinVo> collect = page.getRecords().stream().map(tbSpecialClockinVo -> {
			QueryWrapper<TbUserInfo> tbUserInfoQueryWrapper = new QueryWrapper<>();
			tbUserInfoQueryWrapper.eq("tb_wxuuid", tbSpecialClockinVo.getTbUserid());
			TbUserInfo tbUserInfo = tbUserInfoService.getOne(tbUserInfoQueryWrapper);
			QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<TbImagesl>().eq("tb_special_uuid", tbSpecialClockinVo.getTbUuid());
			List<TbImagesl> tbImageslList = tbImageslService.list(tbImageslQueryWrapper);
			QueryWrapper<TbLike> likeQueryWrapper = new QueryWrapper<TbLike>().eq("target_id", tbSpecialClockinVo.getTbId());
			long count = tbLikeService.count(likeQueryWrapper);
			QueryWrapper<TbComment> tbCommentQueryWrapper = new QueryWrapper<TbComment>().eq("tb_target_id", tbSpecialClockinVo.getTbId());
			Long counted = tbCommentService.count(tbCommentQueryWrapper);
			tbSpecialClockinVo.setTbUsername(tbUserInfo.getTbUsername());
			tbSpecialClockinVo.setTbImage(tbUserInfo.getTbImage());
			tbSpecialClockinVo.setLikeNum(count);
			tbSpecialClockinVo.setCommentNum(counted);
			tbSpecialClockinVo.setTbImageslList(tbImageslList);
			return tbSpecialClockinVo;
		}).sorted((o1, o2) -> o2.getTbCreatetime().compareTo(o1.getTbCreatetime())).collect(Collectors.toList());
		pageUtils.setList(collect);
		return pageUtils;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void removetbSpecialClockinByIds(List<Integer> list) {
		for (int i = 0; i < list.size(); i++) {
			QueryWrapper<TbSpecialClockin> tbSpecialClockinQueryWrapper = new QueryWrapper<TbSpecialClockin>().eq("tb_id", list.get(i));
			TbSpecialClockin tbSpecialClockin = this.getOne(tbSpecialClockinQueryWrapper);
			QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<>();
			tbImageslQueryWrapper.eq("tb_special_uuid", tbSpecialClockin.getTbUuid());
			List<TbImagesl> tbImagesls = tbImageslService.list(tbImageslQueryWrapper);
			for (int j = 0; j < tbImagesls.size(); j++) {
				this.removeById(list.get(i));
				String uploadDir = fileUploadUtil.getWinOrLinuxUploadFilePath("tp");
				// 创建 File 对象
				File file = new File(tbImagesls.get(j).getTbImage());

				// 获取文件名（不包含路径）
				String fileName = file.getName();
				// 替换为你实际的文件路径
				String filePath = uploadDir + fileName;
				try {
					File file1 = new File(filePath);
					file1.delete();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			tbImageslService.remove(tbImageslQueryWrapper);
			QueryWrapper<TbComment> tbCommentQueryWrapper = new QueryWrapper<>();
			tbCommentQueryWrapper.eq("tb_target_id", tbSpecialClockin.getTbId()).and(tbCommentQueryWrapper1 -> {
				tbCommentQueryWrapper1.eq("tb_type", 1);
			});
			tbCommentService.remove(tbCommentQueryWrapper);
			QueryWrapper<TbLike> tbLikeQueryWrapper = new QueryWrapper<>();
			tbLikeQueryWrapper.eq("target_id", tbSpecialClockin.getTbUuid());
			tbLikeService.remove(tbLikeQueryWrapper);
		}
	}

	@Override
	public R UnderZtList(Param params) {
		PageUtils pageUtils = null;
		// boolean hasKey = redisUtils.hasKey(getmyZtClockin);
		// if (!hasKey) {
		Map<String, Object> param = new HashMap<>();
		String page1 = params.getPage();
		String limit1 = params.getLimit();
		param.put("page", page1);
		param.put("limit", limit1);
		MPJLambdaWrapper<TbSpecialClockin> tbSpecialClockinVoMPJLambdaWrapper = new MPJLambdaWrapper<>();
		tbSpecialClockinVoMPJLambdaWrapper.select(TbUserInfo::getTbUsername, TbUserInfo::getTbImage).select(TbSpecialClockin::getTbId, TbSpecialClockin::getTbUuid, TbSpecialClockin::getTbLocation, TbSpecialClockin::getTbCreatetime, TbSpecialClockin::getTbLongitudeLatitude, TbSpecialClockin::getTbStatus, TbSpecialClockin::getTbTestimonials, TbSpecialClockin::getTbSpecialid).select(TbSpecial::getTbSpecialname, TbSpecial::getTbId).leftJoin(TbUserInfo.class, TbUserInfo::getTbWxuuid, TbSpecialClockin::getTbUserid).leftJoin(TbSpecial.class, TbSpecial::getTbId, TbSpecialClockin::getTbSpecialid).eq(TbSpecialClockin::getTbStatus, 2).and(tw -> tw.eq(TbSpecialClockin::getTbSpecialid, params.getSpecialid()));
		if (params.getTbCreatetime() != null && !"".equals(params.getTbCreatetime())) {
			tbSpecialClockinVoMPJLambdaWrapper.and(tw -> tw.eq(TbSpecialClockin::getTbCreatetime, params.getTbCreatetime()));
		}
		IPage<TbSpecialClockinVo> page = tbSpecialClockinMapper.selectJoinPage(new Page<>(Long.parseLong((String) param.get("page")), Long.parseLong((String) param.get("limit"))), TbSpecialClockinVo.class, tbSpecialClockinVoMPJLambdaWrapper);
		if (params.getTbCreatetime() != null && !"".equals(params.getTbCreatetime())) {
			tbSpecialClockinVoMPJLambdaWrapper.eq(TbSpecialClockin::getTbCreatetime, params.getTbCreatetime());
		}
		pageUtils = new PageUtils(page);
		List<TbSpecialClockinVo> collect = page.getRecords().stream().map(tbSpecialClockinVo -> {
			QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<TbImagesl>().eq("tb_special_uuid", tbSpecialClockinVo.getTbUuid());
			List<TbImagesl> tbImageslList = tbImageslService.list(tbImageslQueryWrapper);
			QueryWrapper<TbLike> likeQueryWrapper = new QueryWrapper<TbLike>().eq("target_id", tbSpecialClockinVo.getTbId());
			long count = tbLikeService.count(likeQueryWrapper);
			QueryWrapper<TbComment> tbCommentQueryWrapper = new QueryWrapper<TbComment>().eq("tb_target_id", tbSpecialClockinVo.getTbId());
			Long counted = tbCommentService.count(tbCommentQueryWrapper);
			tbSpecialClockinVo.setLikeNum(count);
			tbSpecialClockinVo.setCommentNum(counted);
			tbSpecialClockinVo.setTbImageslList(tbImageslList);
			return tbSpecialClockinVo;
		}).sorted((o1, o2) -> o2.getTbCreatetime().compareTo(o1.getTbCreatetime())).collect(Collectors.toList());
		pageUtils.setList(collect);
		return R.ok().put("page", pageUtils);
	}

	@Override
	public R getTbSpecialClockinById(Integer tbId) {
		String openId = JwtUtils.getOpenId(JwtUtils.getTokenFromRequest(request));
		TbSpecialClockin tbSpecialClockin = this.getOne(new QueryWrapper<TbSpecialClockin>().eq("tb_id", tbId));
		List<TbImagesl> list = null;
		TbSpecialClockinVo tbSpersonClockinVo = null;
		if (tbSpecialClockin != null) {
			QueryWrapper<TbUserInfo> tbUserInfoQueryWrapper = new QueryWrapper<>();
			TbUserInfo userInfo = tbUserInfoService.getOne(tbUserInfoQueryWrapper.eq("tb_wxuuid", tbSpecialClockin.getTbUserid()));
			QueryWrapper<TbSpecial> tbSpecialQueryWrapper = new QueryWrapper<>();
			tbSpecialQueryWrapper.eq("tb_id", tbSpecialClockin.getTbSpecialid());
			TbSpecial serviceOne = tbSpecialService.getOne(tbSpecialQueryWrapper);
			QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<TbImagesl>().eq("tb_special_uuid", tbSpecialClockin.getTbUuid());
			list = tbImageslService.list(tbImageslQueryWrapper);
			QueryWrapper<TbLike> likeQueryWrapper = new QueryWrapper<TbLike>().eq("target_id", tbSpecialClockin.getTbUuid()).and(tw -> tw.eq("user_id", openId));
			TbLike tbLike = tbLikeService.getOne(likeQueryWrapper);
			QueryWrapper<TbLike> likeQueryWrapper1 = new QueryWrapper<TbLike>().eq("target_id", tbSpecialClockin.getTbUuid());
			long count = tbLikeService.count(likeQueryWrapper1);
			QueryWrapper<TbComment> commentQueryWrapper1 = new QueryWrapper<TbComment>().eq("tb_target_id", tbSpecialClockin.getTbId()).and(tbCommentQueryWrapper1 -> {
				tbCommentQueryWrapper1.eq("tb_type", 1);
			}).and(tw -> tw.eq("tb_target_status", 1));
			tbSpersonClockinVo = new TbSpecialClockinVo();
			tbSpersonClockinVo.setTbId(tbSpecialClockin.getTbId());
			tbSpersonClockinVo.setTbCreatetime(tbSpecialClockin.getTbCreatetime());
			tbSpersonClockinVo.setTbSpecialname(serviceOne.getTbSpecialname());
			tbSpersonClockinVo.setTbUuid(tbSpecialClockin.getTbUuid());
			tbSpersonClockinVo.setTbImage(userInfo.getTbImage());
			tbSpersonClockinVo.setTbUsername(userInfo.getTbUsername());
			tbSpersonClockinVo.setTbStatus(tbSpecialClockin.getTbStatus());
			tbSpersonClockinVo.setTbLocation(tbSpecialClockin.getTbLocation());
			tbSpersonClockinVo.setTbLongitudeLatitude(tbSpecialClockin.getTbLongitudeLatitude());
			tbSpersonClockinVo.setTbTestimonials(tbSpecialClockin.getTbTestimonials());
			tbSpersonClockinVo.setLikeNum(count);
			tbSpersonClockinVo.setCommentNum(tbCommentService.count(commentQueryWrapper1));
			tbSpersonClockinVo.setTbImageslList(list);
			// tbSpersonClockinVo.setTbCommentList(tbCommentService.list(commentQueryWrapper));
			// List<TbComment> list1 = tbCommentService.list(commentQueryWrapper2);
			if (tbLike != null) {
				tbSpersonClockinVo.setClockStatus(true);
			} else {
				tbSpersonClockinVo.setClockStatus(false);
			}
			// String tbImageids = tbPersonClockin.getTbImageids();
			// if (tbImageids != null && !"".equals(tbImageids)) {
			//     String[] tbImageid = tbImageids.split(",");
			//     QueryWrapper<TbImagesl> imageslQueryWrapper = new QueryWrapper<TbImagesl>().in("tb_id", tbImageid);
			//     list = tbImageslService.list(imageslQueryWrapper);
			// }
		}
		return R.ok().put("tbSpecialClockinVo", tbSpersonClockinVo);
	}

	@Override
	public PageUtils dataList(Param param) {
		String openId = JwtUtils.getOpenId(JwtUtils.getTokenFromRequest(request));
		int page1 = Integer.parseInt(param.getPage());
		int limit = Integer.parseInt(param.getLimit());
		page1 = (page1 - 1) * limit;
		List<TbSpecialClockinVo> tbSpecialClockinList = tbspecialclockinMapper.selectUnionClockin(page1, limit);
		int count = tbspecialclockinMapper.selectUnionClockinCount();
		List<TbSpecialClockinVo> collect = tbSpecialClockinList.stream().map(tbSpecialClockin -> {
			TbSpecialClockinVo tbSpecialClockinVo = new TbSpecialClockinVo();
			BeanUtils.copyProperties(tbSpecialClockin, tbSpecialClockinVo);
			QueryWrapper<TbUserInfo> userInfoQueryWrapper = new QueryWrapper<TbUserInfo>().eq("tb_wxuuid", tbSpecialClockin.getTbUserid());
			TbUserInfo tbUserInfo = tbUserInfoService.getOne(userInfoQueryWrapper);
			QueryWrapper<TbLike> tbLikeQueryWrapper = new QueryWrapper<>();
			QueryWrapper<TbSpecial> tbSpecialQueryWrapper = new QueryWrapper<>();
			TbSpecial tbSpecial = null;
			if (tbSpecialClockin.getTbSpecialid() != null) {
				tbSpecialQueryWrapper.eq("tb_id", tbSpecialClockin.getTbSpecialid());
				tbSpecial = tbSpecialService.getOne(tbSpecialQueryWrapper);
			}
			List<TbImagesl> tbImageslList = null;
			if (tbSpecialClockin.getTbSpecialid() != null) {
				QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<TbImagesl>().eq("tb_special_uuid", tbSpecialClockin.getTbUuid());
				tbImageslList = tbImageslService.list(tbImageslQueryWrapper);
			}else{
				QueryWrapper<TbImagesl> tbImageslQueryWrapper = new QueryWrapper<TbImagesl>().eq("tb_person_uuid", tbSpecialClockin.getTbUuid());
				tbImageslList = tbImageslService.list(tbImageslQueryWrapper);
			}

			QueryWrapper<TbLike> likeQueryWrapper = new QueryWrapper<TbLike>().eq("target_id", tbSpecialClockin.getTbUuid());
			long count1 = tbLikeService.count(likeQueryWrapper);
			QueryWrapper<TbComment> tbCommentQueryWrapper = new QueryWrapper<TbComment>().eq("tb_target_id", tbSpecialClockin.getTbId()).and(tw -> {
				tw.eq("tb_type", 1);
			}).and(tw -> tw.eq("tb_target_status", 1));
			Long counted = tbCommentService.count(tbCommentQueryWrapper);
			QueryWrapper<TbLike> and = tbLikeQueryWrapper.eq("target_id", tbSpecialClockin.getTbUuid()).and(tbLikeQueryWrapper1 -> {
				tbLikeQueryWrapper1.eq("user_id", openId);
			});
			TbLike tbLike = tbLikeService.getOne(and);
			if (tbLike != null) {
				tbSpecialClockinVo.setClockStatus(true);
			} else {
				tbSpecialClockinVo.setClockStatus(false);
			}
			tbSpecialClockinVo.setTbUsername(tbUserInfo.getTbUsername());
			tbSpecialClockinVo.setTbImage(tbUserInfo.getTbImage());
			if (tbSpecial != null) {
				tbSpecialClockinVo.setTbSpecialname(tbSpecial.getTbSpecialname());
			}else{
				tbSpecialClockinVo.setTbSpecialname(null);
			}
			tbSpecialClockinVo.setTbImageslList(tbImageslList);
			tbSpecialClockinVo.setLikeNum(count1);
			tbSpecialClockinVo.setCommentNum(counted);
			tbSpecialClockinVo.setTbImageslList(tbImageslList);
			return tbSpecialClockinVo;
		}).sorted((o1, o2) -> o2.getTbCreatetime().compareTo(o1.getTbCreatetime())).collect(Collectors.toList());
		PageUtils page = new PageUtils(collect, count, limit, Integer.parseInt(param.getPage()));
		return page;
	}

}